package com.apes.fn.server.workOrder.service;

import com.apes.fn.server.workOrder.model.ConstructionRecord;
import com.apes.fn.server.workOrder.model.MaintenanceOrder;
import com.apes.fn.server.workOrder.repository.ConstructRecordRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.enumeration.repository.EnumerationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Creator: zouyc
 * CreateDate: 2020-04-15 18:59
 * Description:
 **/
@Service("constructionRecordService")
public class ConstructionRecordService extends DomainService {
    @Autowired
    ConstructRecordRepository constructRecordRepository;

    /**
     * 施工记录
     * @param maintenanceOrder
     * @return
     */
    public ConstructionRecord constructionRecord(MaintenanceOrder maintenanceOrder,String type,String reasonId){
        ConstructionRecord constructionRecord = new ConstructionRecord();
        constructionRecord.setLastConstructionRecord(findLastRecordByMaintenorder(maintenanceOrder));
        constructionRecord.setMaintenanceOrder(maintenanceOrder);
        constructionRecord.setOperateType(type);
        if(reasonId != null && !"".equals(reasonId)) constructionRecord.setOperateReason(SpringManager.getBean(EnumerationRepository.class).findOne(reasonId));
        return constructionRecord;
    }

    /**
     * 施工记录(钣喷)
     * @param maintenanceOrder
     * @return
     */
    public void constructionRecordForBP(MaintenanceOrder maintenanceOrder,Date start,Date end){
        ConstructionRecord constructionRecord = new ConstructionRecord();
        constructionRecord.setLastConstructionRecord(findLastRecordByMaintenorder(maintenanceOrder));
        constructionRecord.setMaintenanceOrder(maintenanceOrder);
        constructionRecord.setOperateType(ConstructionRecord.CONST_TABLE_OPERATETYPE_START);
        constructionRecord.setCreateDate(start);
        constructRecordRepository.saveAndFlush(constructionRecord);

        ConstructionRecord endConstructionRecord = new ConstructionRecord();
        endConstructionRecord.setLastConstructionRecord(findLastRecordByMaintenorder(maintenanceOrder));
        endConstructionRecord.setMaintenanceOrder(maintenanceOrder);
        endConstructionRecord.setOperateType(ConstructionRecord.CONST_TABLE_OPERATETYPE_FINISH);
        endConstructionRecord.setCreateDate(end);
        ConstructionRecord finishConstruc= constructRecordRepository.saveAndFlush(endConstructionRecord);

        SpringManager.getBean(MaintenanceOrderService.class).calcutelateTimeNow(finishConstruc,maintenanceOrder);
    }

    /**
     * 新增
     * @param simpleRequest
     * @return
     */
    public ConstructionRecord create(SimpleRequest simpleRequest){
        ConstructionRecord constructionRecord = simpleRequest.getO(ConstructionRecord.class);
        constructionRecord = save(constructionRecord);
        return constructionRecord;
    }

    /**
     * 查找上一步操作
     * @param maintenanceOrder
     * @return
     */
    public ConstructionRecord findLastRecordByMaintenorder(MaintenanceOrder maintenanceOrder){
        List<ConstructionRecord> list
                = constructRecordRepository.findAll(JpaDsl.toCriteriaByEq("maintenanceOrder.id",maintenanceOrder.getId()),new Sort(Sort.Direction.DESC, "id"));

        return list.stream().findFirst().orElse(null);
    }

    /**
     * 保存操作
     * @param
     * @return
     */
    private ConstructionRecord save(ConstructionRecord constructionRecord){
        return constructRecordRepository.saveAndFlush(constructionRecord);
    }

    /**
     *返回施工时长
     * @param maintenanceOrder
     * @return
     */
    public Integer caculateTimeByMaintenanceOrder(MaintenanceOrder maintenanceOrder){
        List<String> list = new ArrayList<>();
        list.add("finish");
        list.add("pause");
        List<ConstructionRecord> constructionRecords = constructRecordRepository.findConstructionRecordForCalculate(maintenanceOrder.getId(),list);

        if(constructionRecords == null ||constructionRecords.size() == 0) return 0;
        int resultSecond = 0;
        for(ConstructionRecord constructionRecord : constructionRecords){
            resultSecond = resultSecond + (int)((constructionRecord.getCreateDate().getTime() -constructionRecord.getLastConstructionRecord().getCreateDate().getTime())/1000);
        }
        return resultSecond;
    }

    /**
     * 终止的时候判断是否有开始记录，有的话，需要写一条终止明细
     * 是否有开始记录
     */
    public boolean checkHaveStartRecord(MaintenanceOrder maintenanceOrder){
        List<ConstructionRecord> constructionRecords =
                constructRecordRepository.findAll(JpaDsl.toCriteriaByEq("maintenanceOrder.id",maintenanceOrder.getId(),"operateType","start"));
        if(constructionRecords.size() != 0) return true;
        return false;
    }

}